/**
 * Created by Eoly on 2017/2/16.
 * Call http://palerock.cn
 */
var userCheckPage = angular.module('userCheckPage', ['ngRoute']);

userCheckPage.controller('userCheck', function ($scope) {
    $scope.suiMobileInit = function () {
        app.suiInit();
        app.adaptPhone();
        loginInit();
    };
});

/**
 * 登录页面
 */
userCheckPage.controller('login', function ($scope, $http) {
    //用户电话号码
    $scope.userPhoneNumber = '';
    //用户登录密码
    $scope.userLoginPsw = '';
    $scope.login = function () {

        var loginInfo = {'userPhoneNumber': $scope.userPhoneNumber, 'userLoginPsw': $scope.userLoginPsw};
        $.showPreloader();
        $http({
            method: 'POST',
            url: net + '/login',
            data: loginInfo
        }).then(function (res) {
            console.log(res);
            $.hidePreloader();
            if (!res.data.error) {
                //设置本地缓存
                localStorage.setItem("ptsc_user_token", res.data.token);

                console.log(res.data);
                alert(1);
                if (!app.backLastPosition()) {
                    location.href = 'index.html#mine';
                }
            } else {
                console.log("错误代码:" + res.data.error + "," + res.data.mes);
                $.alert(res.data.mes, function () {
                });
            }
        }, function () {
            $.hidePreloader();
            $.toast('网络错误');
        });
    }
});

/**
 * 注册页面
 */
userCheckPage.controller('register', function ($scope, $http) {
    //用户电话号码
    $scope.userPhoneNumber = '';
    //用户登录密码
    $scope.userLoginPsw = '';
    //再一次输入密码
    $scope.userLoginPswAgain = '';

    $scope.registerChecking = function () {

        if($scope.userPhoneNumber == ''||$scope.userLoginPsw == ''||$scope.userLoginPswAgain == ''){
            $.toast('手机号，密码，确认密码都不能为空');
            return false
        }

        if(!(/^1[3|4|5|8][0-9]\d{8}$/.test($scope.userPhoneNumber))){
            // alert($scope.userPhoneNumber);
            $.toast('手机号格式不正确');
            return false
        }

        if($scope.userLoginPsw!=$scope.userLoginPswAgain){
            $.toast('两次密码输入不同');
            return false
        }

        return true;
    };

    $scope.registering = function () {

        if(!$scope.registerChecking()){
            return;
        }

        var registorInfo = {'userPhoneNumber': $scope.userPhoneNumber, 'userLoginPsw': $scope.userLoginPsw};
        $.showPreloader();
        $http({
            method: 'POST',
            url: net + '/register',
            data: registorInfo
        }).then(function (res) {
            console.log(res);
            $.hidePreloader();
            if (!res.data.error) {
                $.alert('注册成功，去登录', function () {
                    app.turnHref('user-check.html', '#register', 'user-check.html');
                });
            } else {
                console.log("错误代码:" + res.data.error + "," + res.data.mes);
                $.alert(res.data.mes, function () {
                });
            }
        }, function () {
            $.hidePreloader();
            $.toast('网络错误');
        });
    }
});

/**
 * 路由
 */
userCheckPage.config(function ($routeProvider) {
    $routeProvider.when('/login', {
        templateUrl: 'html/login-content.html',
        controller: 'login'
    }).when('/register', {
        templateUrl: 'html/register-content.html',
        controller: 'register'
    }).otherwise({
        redirectTo: '/login'
    });
});